package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.MedicinalCategroy;
import com.his.service.MedicinalCategroyService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * (MedicinalCategroy)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:12:56
 */
@RestController
@RequestMapping("medicinalCategroy")
@Validated
public class MedicinalCategroyController {
    /**
     * 服务对象
     */
    @Resource
    private MedicinalCategroyService medicinalCategroyService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequireRoles(roles = {"medicinal", "doctor"}, index = 0)
    @GetMapping("selectOne")
    public MedicinalCategroy selectOne(@RequestHeader(Const.TOKEN) String token, @NotNull(message = "主键不能为空") Integer id) {
        return this.medicinalCategroyService.queryById(id);
    }

    /**
     * 增加药品类别
     */
    @RequireRoles(roles = "medicinal", index = 0)
    @PostMapping("insertOne")
    public Rsp insertOne(@RequestHeader(Const.TOKEN) String token, @RequestBody MedicinalCategroy medicinalCategroy) {
        try {
            medicinalCategroy.setCreateId(MyUtil.getUserByToken(token));//设置创建人ID
        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加药品种类
        return medicinalCategroyService.insert(token, medicinalCategroy);
    }

    /*
     *删除药品类别
     */
    @RequireRoles(roles = "medicinal", index = 0)
    @DeleteMapping("deleteOne")
    public Rsp deleteByIdLogic(@RequestHeader(Const.TOKEN) String token, @RequestBody MedicinalCategroy medicinalCategroy) {
        return medicinalCategroyService.deleteByIdLogic(medicinalCategroy.getId());
    }

    /**
     * 修改药品类别
     */
    @RequireRoles(roles = "medicinal", index = 0)
    @PutMapping("updateOne")
    public Rsp updateOne(@RequestHeader(Const.TOKEN) String token,
                         @RequestBody MedicinalCategroy medicinalCategroy) {

        return medicinalCategroyService.update(token, medicinalCategroy);
    }

    /**
     * 查询类别列表
     */
    @RequireRoles(roles = "medicinal", index = 0)
    @PostMapping("searchSomeMedicinalCategroyPage")
    public Rsp searchSomeMedicinalCategroyPage(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<MedicinalCategroy> pageParam) {
        return Rsp.success(medicinalCategroyService.queryAll(pageParam));
    }

    /**
     * 查询所有类别(不分页)
     */
    @RequireRoles(roles = {"medicinal", "doctor"}, index = 0)
    @PostMapping("searchCategroyList")
    public Rsp searchCategroyList(@RequestHeader(Const.TOKEN) String token) {
        return medicinalCategroyService.searchCategroyList();
    }
}
